$(function () {
    $('.register_checkout li[attr="register"]').addClass('active')
    $('.register_checkout li').click(function () {
        var attr = $(this).attr('attr')
        $(this).addClass('active').siblings().removeClass('active')
        if (attr == 'register') {
            $(`.register_sumit`).show()
            $(`.reset_sumit`).hide()
        } else {
            $(`.register_sumit`).hide()
            $(`.reset_sumit`).show()
        }
    })

    // token
    var token = ''
    var phone = ''
    var code = ''
    var smscode = ''
    var password = ''
    var password_repeat = ''
    function getFormVal() {
        // 获取表格值
        phone = $('.register input[name="phone"]').val()
        code = $('.register input[name="code"]').val()
        smscode = $('.register input[name="smscode"]').val()
        password = $('.register input[name="password"]').val()
        password_repeat = $('.register input[name="password_repeat"]').val()
        console.log(phone, code, password, password_repeat)
    }

    // 生成验证码
    function captcha() {
        $.ajax({
            url: '/captcha',
            success: function (res) {
                if (res.code == 1) {
                    $('#svg').html(res.data)
                    token = res.token
                } else {
                    alert(res.msg || '验证码获取失败')
                }
            }
        })
    }
    captcha() // 初始化
    $('#svg').click(function () {
        // 点击切换
        captcha()
    })

    // 发送验证码
    var issending = false // 是否在发送中
    $('#send_text').click(function () {
        getFormVal()
        if (issending) {
            return
        }
        if (phone.length != 11) {
            return alert('手机号格式不正确')
        }
        if (code.length != 6) {
            return alert('图片验证码格式不正确')
        }
        $.ajax({
            url: this.is_register ? '/sms' : '/reset_sms',
            method: 'GET',
            data: {
                token,
                code,
                phone
            },
            success: function (res) {
                if (res.code == 1) {
                    issending = true
                    alert(res.msg)
                    let i = 60
                    let timer = setInterval(() => {
                        i--
                        $('#send_text').text(i + ' S')
                        if (i <= 0) {
                            $('#send_text').text('发送验证码')
                            clearInterval(timer)
                        }
                    }, 1000)
                } else {
                    alert(res.msg)
                }
            }
        })
    })

    // 提交注册信息
    $('.submit').click(function () {
        getFormVal()
        if (phone.length != 11) {
            return alert('手机号格式不正确')
        }
        if (smscode.length != 6) {
            return alert('短信验证码不正确')
        }
        if (password != password_repeat) {
            return alert('两次密码不一致')
        }
        if (password < 6 || password_repeat.length > 12) {
            return alert('密码格式不正确,只允许6-12位')
        }
        $.ajax({
            url: '/reg',
            method: 'POST',
            contentType: 'application/json',
            data: JSON.stringify({
                phone,
                pass: password,
                smscode: smscode
            }),
            success: function (res) {
                if (res.code == 1) {
                    alert(res.msg)
                    $('.mask').hide()
                } else {
                    alert(res.msg)
                }
            }
        })
    })

    // 重置密码
    var istimer = false
    $('.reset_sumit').click(function () {
        getFormVal()
        if (istimer) {
            return alert('禁止操作')
        }
        if (phone.length != 11) {
            return alert('手机号格式不正确')
        }
        if (smscode.length != 6) {
            return alert('短信验证码不正确')
        }
        if (password != password_repeat) {
            return alert('两次密码不一致')
        }
        if (password < 6 || password_repeat.length > 12) {
            return alert('密码格式不正确,只允许6-12位')
        }
        istimer = true
        let data = {
            phone: phone,
            pass: password,
            smscode
        }
        $.ajax({
            url: '/ref',
            method: 'POST',
            contentType: 'application/json',
            data: JSON.stringify(data),
            success: function (res) {
                if (res.code == 1) {
                    alert(res.msg)
                } else {
                    alert(res.msg)
                }
            },
            complete: function () {
                let i = 60
                let timer = setInterval(() => {
                    i--
                    if (i == 0) {
                        clearInterval(timer)
                    }
                }, 1000)
            }
        })
    })
})
